﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceProcess;
using ESF.General;

#endregion


namespace ESF.WCF
{
	public class BaseWCFWindowsService<T> : BaseWindowsService where T : new ()
	{
		#region Miembros
		
		private ServiceHost _ServiceHost;
		
		#endregion
		
	
		#region Constructor
		
		public BaseWCFWindowsService()
		{
			this.ServiceName = "WCF Windows Service";
			
			_ServiceHost = new ServiceHost (typeof (T));
		}
		
		#endregion
		
		
		#region Funciones
		
		public override void DoStart ()
		{
			Console.Write(" - Opening Host...");

			if (_ServiceHost == null)
			{
				Console.WriteLine(" [ NOT DEFINED ]");
				return;
			} 
			
			if (_ServiceHost.State == CommunicationState.Opened || _ServiceHost.State == CommunicationState.Opening)
			{
				Console.WriteLine ("[ Already open ]"); 
				return;			
			}
			

			_ServiceHost.Open ();
			
			Console.WriteLine (" [ OK ]");
		}
		
		
		public override void DoStop ()
		{
			Console.Write (" - Closing Host...");
		
			if (_ServiceHost == null)
			{
				Console.WriteLine (" [ NOT DEFINED ]");
				return;
			}
		
			if (_ServiceHost.State == CommunicationState.Closed || _ServiceHost.State == CommunicationState.Closing)
			{
				Console.WriteLine ("[ Already closed ]");
				return;
			}
			
			_ServiceHost.Close ();
			
			Console.WriteLine (" [ OK ]");	
		}
		
		#endregion
	}
}
